var vm = new Vue({
    el: '#rrapp',
    data: {
        title: '',
        loading: false,
        goodsRecommend: [],
        goodsList: [],
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0}, //分页(定义)
        q: {
            goodsId: '',
            startTime: '',
            endTime: ''
        },
        //新增/编辑
        remmendFrom: {
            goodsId: '',
            showSort: ''
        },
        dialogFormVisible: false,
        //重置按钮
        resetForm(remmendFrom) {
            this.$refs[remmendFrom].resetFields();
        }
    },
    created: function () {  //加载数据
        this.loadData();
    },
    methods: {
        //主页数据
        loadData: function () {
            //  debugger;
            var params = this.q;
            params.page = this.page.currentPage;
            params.limit = this.page.pageSize;
            $.ajax({
                type: "POST",
                url: baseURL + "goods/goodsrecommend/list",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(params),
                success: function (r) {
                    if (r.code == 0) {
                        vm.goodsRecommend = r.page.list;
                        vm.page.total = r.page.totalCount;
                        vm.goodsList = r.goodsList;
                    } else {
                        alert(r.msg);
                    }
                }
            })
        },
        //每页显示数据量变更
        handleSizeChange: function (val) {
            vm.page.pageSize = val;
            this.loadData();
        },
        //当前页面
        handleCurrentChange: function (val) {
            vm.page.currentPage = val;
            this.loadData();
        },
        //恢复按钮
        reset: function () {
            vm.q.goodsId = '';
            vm.q.startTime = '';
            vm.q.endTime = '';
            this.loadData();
        },
        //搜索
        search: function () {
            this.page.currentPage = 1;
            this.loadData();
        },
        //序号
        indexMethod(index) {
            return index + 1;
        },

        //远程搜索
        remoteMethod(query) {
            if (query !== '') {
                var item = {'name': query};
                var _this = this;
                _this.loading = true;
                var url = 'goods/goodsrecommend/search';
                setTimeout(function () {
                    _this.loading = false;
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(item),
                        success: function (r) {
                            if (r.code === 0) {
                                vm.goodsList = r.goodsList;
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }, 200)
            }
        },
        ///////////

        //取消清空验证
        closeChangeMachie() {
            vm.dialogFormVisible = false;
            this.$refs['remmendFrom'].resetFields();
        },
        add: function () {
            this.title = "新 增";
            vm.remmendFrom = {};
            this.dialogFormVisible = true;
        },
        handleEdit: function (index, row) {
            var rowData = JSON.parse(JSON.stringify(row));
            vm.remmendFrom = Object.assign({}, rowData);
            this.title = "修 改";
            vm.dialogFormVisible = true;
        },
        //新增编辑提交
        submitForm(remmendFrom) {
            var postData = {id: vm.remmendFrom.id, goodsId: vm.remmendFrom.goodsId, showSort: vm.remmendFrom.showSort};
            var url;
            var _this=this;
            if (isBlank(postData.id)) {
                url = "goods/goodsrecommend/save";
            } else {
                url = "goods/goodsrecommend/update";
            }
            this.$refs[remmendFrom].validate(function (valid) {
                if (valid) {
                    //输入为负数，不写入数据库
                    if (!validNumber(vm.remmendFrom.showSort)) {
                        alert("推荐请输入大于0的整数数字");
                        return false;
                    }
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            //console.log(JSON.stringify(postData));
                            if (r.code === 0) {
                                vm.dialogFormVisible = false;
                                _this.$message({
                                    type: 'success',
                                    message: '操作成功!'
                                });
                                setTimeout(function () {
                                    vm.loadData();
                                }, 1000);
                            } else {
                                alert(r.msg);
                            }
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        ///////////////////
        handleDelete: function (index, row) {
            var rowData = JSON.parse(JSON.stringify(row));
            var id = rowData.id;
            var _this = this;
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "goods/goodsrecommend/delete?id="+id,
                    contentType: "application/json",
                    data:null,
                    success: function (r) {
                        if (r.code == 0) {
                            _this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            setTimeout(function () {
                                vm.loadData()
                            }, 1000)
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }).catch(function () {
                _this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    }
});

//验证数字为大于0的非负整数
function validNumber(str) {
    var reg = /^\+?[1-9][0-9]*$/;
    if (!reg.test(str)) {
        return false;
    } else {
        return true;
    }
}

//控制提交间隔3s
function testButton() {
    var aaa = document.getElementById('click1');
    aaa.disabled = true; //使用true或false，控制是否让按钮禁用
    setTimeout(function () {
        aaa.disabled = false;
    }, 2500);//3秒后才能点击
}

